package org.idansof.otp.ui;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.idansof.otp.client.Itinerary;
import org.idansof.otp.client.Leg;
import org.idansof.otp.client.Location;
import org.idansof.otp.client.Leg.Mode;
import org.mapsforge.android.maps.overlay.ArrayItemizedOverlay;
import org.mapsforge.android.maps.overlay.ArrayWayOverlay;
import org.mapsforge.android.maps.GeoPoint;
import org.mapsforge.android.maps.overlay.ItemizedOverlay;
import org.mapsforge.android.maps.MapActivity;
import org.mapsforge.android.maps.MapFragment;
import org.mapsforge.android.maps.MapView;
import org.mapsforge.android.maps.MapViewMode;
import org.mapsforge.android.maps.overlay.OverlayItem;
import org.mapsforge.android.maps.overlay.OverlayWay;

import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class DisplayMapFragment extends MapFragment {

	private Leg leg;
	private Location location;
	private Itinerary itinerary;
	
	private Map<Leg.Mode, Paint> legPaints = new HashMap<Mode, Paint>();
	
	private void initLegPaints()
	{
        Paint wayWalkPaintFill = new Paint(Paint.ANTI_ALIAS_FLAG);
        wayWalkPaintFill.setStyle(Paint.Style.STROKE);
        wayWalkPaintFill.setColor(Color.RED);
        wayWalkPaintFill.setAlpha(160);
        wayWalkPaintFill.setStrokeWidth(4);
        wayWalkPaintFill.setStrokeJoin(Paint.Join.ROUND);
        wayWalkPaintFill.setPathEffect(new DashPathEffect(new float[] { 20, 20 }, 0));
        legPaints.put(Leg.Mode.WALK, wayWalkPaintFill);

        Paint wayBusPaintFill = new Paint(Paint.ANTI_ALIAS_FLAG);
        wayBusPaintFill.setStyle(Paint.Style.STROKE);
        wayBusPaintFill.setColor(Color.BLUE);
        wayBusPaintFill.setAlpha(160);
        wayBusPaintFill.setStrokeWidth(7);
        wayBusPaintFill.setStrokeJoin(Paint.Join.ROUND);
        legPaints.put(Leg.Mode.BUS, wayBusPaintFill);
        legPaints.put(Leg.Mode.TRAM, wayBusPaintFill);
		
        Paint wayRailPaintFill = new Paint(Paint.ANTI_ALIAS_FLAG);
        wayRailPaintFill.setStyle(Paint.Style.STROKE);
        wayRailPaintFill.setColor(Color.GREEN);
        wayRailPaintFill.setAlpha(160);
        wayRailPaintFill.setStrokeWidth(12);
        wayRailPaintFill.setStrokeJoin(Paint.Join.ROUND);
        legPaints.put(Leg.Mode.RAIL, wayRailPaintFill);
        legPaints.put(Leg.Mode.SUBWAY, wayRailPaintFill);
        
		
		
	}
	
	public View onCreateView(LayoutInflater vi, ViewGroup container,
			Bundle savedInstanceState) {
        location = (Location) getArguments().get("location");
		itinerary = (Itinerary) getArguments().get("itinerary");
        
        MapView mapView = new MapView(getMapContext(), MapViewMode.MAPNIK_TILE_DOWNLOAD);
                
        initLegPaints();
        // create the default paint objects for overlay ways
        
        
        Paint wayDefaultPaintOutline = new Paint(Paint.ANTI_ALIAS_FLAG);
        wayDefaultPaintOutline.setStyle(Paint.Style.STROKE);
        wayDefaultPaintOutline.setColor(Color.WHITE);
        wayDefaultPaintOutline.setAlpha(128);
        wayDefaultPaintOutline.setStrokeWidth(4);
        wayDefaultPaintOutline.setStrokeJoin(Paint.Join.ROUND);

        
        ArrayWayOverlay arrayWayOverlay = new ArrayWayOverlay(null,null);
        ArrayItemizedOverlay arrayItemizedOverlay = new ArrayItemizedOverlay(null,false);

        
        for(Leg leg : itinerary.getLegs())
        {
            GeoPoint[][] geoPoints= new GeoPoint[1][];
            geoPoints[0] = new GeoPoint[leg.getGeometry().size()];
            int i=0;
            for(Location location : leg.getGeometry())
            {
            	geoPoints[0][i++] = new GeoPoint(location.getLatitude(), location.getLongitude());        	
            }
            Paint outlinePaint = wayDefaultPaintOutline;
            Paint fillPaint = legPaints.get(leg.getMode());
            if (fillPaint == null)
            	fillPaint = outlinePaint;
            
            arrayWayOverlay.addWay(new OverlayWay(geoPoints,fillPaint,outlinePaint));
        	try {
            	String methodIconPath = "leg/mode/"+leg.getMode().toString().toLowerCase()+".png";
				Drawable methodIconImage = Drawable.createFromStream(getResources().getAssets().open(methodIconPath), methodIconPath);
				Location sourceLocation = leg.getFrom();
				GeoPoint sourceGeopoint = new GeoPoint(sourceLocation.getLatitude(),sourceLocation.getLongitude());
				arrayItemizedOverlay.addItem(new OverlayItem(sourceGeopoint, null,null, ItemizedOverlay.boundCenterBottom(methodIconImage)));
			} catch (IOException e) {
				Log.e(getClass().toString(), "Cannot load icon for "+leg.getMode(),e );
			}
        	
        }
        
        mapView.getOverlays().add(arrayWayOverlay);
        mapView.getOverlays().add(arrayItemizedOverlay);
        
        mapView.setClickable(true);
        mapView.setBuiltInZoomControls(true);
        mapView.getController().setCenter(new GeoPoint(location.getLatitude(), location.getLongitude()));
        return mapView;
}
	
	
}
